package com.summli.basic.class02;

/**
 * 删除链表中给定的元素
 * 整体的思路，next遍历，两个指针，cur：当前Node,pre：cur之前的Node，有一点很重要pre和cur一定不能处于同一点
 */
public class Code02_DeleteGivenValue {
    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }
    public static Node removeValue(Node head, int num) {
        Node pre = head;
        Node cur = head;
        while(cur != null){
            if(cur.value == num){
                pre.next = cur.next;
            }else{
                pre = cur;
            }
            cur = cur.next;
        }
        if(pre.value == num){
            return pre.next;
        }
        return head;
    }
}
